package com.tarena.sentinel.demo02.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import org.springframework.stereotype.Service;

@Service
public class HelloService {
    @SentinelResource(value="resource02",blockHandler = "sayHiError")
    public String sayHi(String name) {
        //慢调用
        try {
            Thread.sleep(150);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hello "+name+",i am from sentinel demo01";
    }
    public String sayHiError(String name, BlockException error) {
        //一旦出现异常,可能是2种规则校验的异常
        //FlowException
        //DegradeException
        if (error instanceof FlowException){
            return "业务层代码sayHi触发了流控规则限制";
        }else if (error instanceof DegradeException){
            return "业务层代码sayHi触发了熔断规则限制";
        }
        return "error";
    }
}
